package com.hok.misc.file.transmission;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

public class MyClient {
	public static void main(String[] args) throws InterruptedException {
		//File file = new File("E:\\film\\tongtiandiguo.avi");
		byte[] buf = new byte[8096];  
		InputStream is = null;
		try {
			is = new FileInputStream(new File("E:\\code-gen.rar"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}  
        BufferedInputStream bis = new BufferedInputStream(is);  
        
        Socket socket = null;  
        try {
			socket = new Socket(InetAddress.getByName("localhost"),9991);
			OutputStream clientToServerStream = socket.getOutputStream();  
            DataInputStream serverToClientStream = new DataInputStream(socket.getInputStream());  
            System.out.println(serverToClientStream.readUTF());  
            int length = -1;
            System.out.println("start tran... " + new Date());
            while((length = bis.read(buf)) != -1){  
                clientToServerStream.write(buf);  
                clientToServerStream.flush();  
            } 
            System.out.println("end tran... " + new Date());
            
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return;
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}   finally {  
            if (socket != null) {  
                try {  
                    socket.close();  
                    System.out.println("close " +  new Date());
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
	}
}
